package com.hghivln.erpwarehouse.system.controller;

import com.hghivln.erpwarehouse.system.common.lang.ActiverUser;
import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import com.hghivln.erpwarehouse.system.common.upload.UploadProperties;
import com.hghivln.erpwarehouse.system.common.upload.UploadService;
import com.hghivln.erpwarehouse.system.entity.User;
import com.hghivln.erpwarehouse.system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@RestController
@RequestMapping("api/file")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private UploadProperties prop;

    @Autowired
    private UserService userService;

    @RequestMapping("doUpload")
    public Object doUpload(MultipartFile mf) {
        String storePath = uploadService.uploadImage(mf);
        String baseUrl = prop.getBaseUrl();
        HashMap<String, Object> map = new HashMap<>();
        map.put("src",baseUrl+storePath);
        ActiverUser activerUser = (ActiverUser) SecurityUtils.getSubject().getPrincipal();
        User user = activerUser.getUser();
        user.setImgpath(storePath);
        userService.updateUser(user);
        return new DataGridlView(map);
    }

    @RequestMapping("uploadGoodsFile")
    public Object uploadGoodsFile(MultipartFile mf) {
        String storePath = uploadService.uploadImage(mf);
        HashMap<String, Object> map = new HashMap<>();
        map.put("src",storePath);
        return new DataGridlView(map);
    }
}
